//package com.example.demo.multidatasource;
//
//import com.example.demo.util.SpringContextUtil;
//import org.apache.log4j.Logger;
//import org.hibernate.SessionFactory;
//import org.hibernate.internal.SessionFactoryImpl;
//import org.springframework.orm.hibernate5.HibernateTransactionManager;
//
//import javax.sql.DataSource;
//import java.util.Set;
//
///**
// * 此类用于创建事物
// *
// * @author Jack
// * @version V1.0
// * @Description: 此类用于创建此类用于创建事物
// * @date 2017/10/24 14:42
// */
//public class MultiTransactionManager extends HibernateTransactionManager {
//    private static final long serialVersionUID = -8255506659278029396L;
//    private transient Logger logger = Logger.getLogger(MultiTransactionManager.class);
//
//    public MultiTransactionManager() {
//
//    }
//
//
//    /**
//     * 获取SessionFactory
//     *
//     * @return SessionFactory
//     * @throws
//     * @Description 首先从MultiSessionFactory中取，没有则使用eminSessionFactory创建
//     * @author Jack
//     * @date 2017/10/24
//     */
//    @Override
//    public SessionFactory getSessionFactory() {
//        MultiSessionFactory multiSessionFactory = SpringContextUtil.getBean("sessionFactory");
//        return multiSessionFactory.getSessionFactory();
//    }
//
//    @Override
//    public void afterPropertiesSet() {
//        logger.info("afterPropertiesSet in MultiTransactionManager and checking the session " +
//                "factory and data source");
//        MultiSessionFactory multiSessionFactory = SpringContextUtil.getBean("sessionFactory");
//        if (multiSessionFactory == null) {
//            logger.error("afterPropertiesSet in MultiTransactionManager session error");
//        }
//        Set<Long> dataSourceKeys = multiSessionFactory.getSessionFactoryKeys();
//        for (Long dataSourceKey : dataSourceKeys) {
//            SessionFactory sessionFactory = multiSessionFactory.getSessionFactory(dataSourceKey);
//            if (sessionFactory == null) {
//                logger.error("afterPropertiesSet in MultiTransactionManager sessionFactory error");
//            }
//            DataSource ds = MultiDataSource.getInstance().getLocalDataSourceByConnectionProvider(
//                    (SessionFactoryImpl) sessionFactory);
//            if (ds == null) {
//                logger.error("afterPropertiesSet in MultiTransactionManager ds error");
//            }
//        }
//    }
//
//    public DataSource getDataSource() {
//        return MultiDataSource.getInstance().getDataSource();
//    }
//
//}
